package com.ihisomp.controller;

import com.ihisomp.base.Result;
import com.ihisomp.dto.OmpUserDto;
import com.ihisomp.service.IOmpUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 标题: AuthController.java
 * 业务描述: 用户验证前端控制类
 * 作者: zy
 * 日期: 2021/9/13
 * 版本: V1.0
 */
@RestController
@Api(value = "用户登录验证", tags = "用户登录验证")
@RequestMapping("/auth")
public class AuthController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IOmpUserService ompUserService;

    /**
     * 用户登录
     *
     * @param ompUserDto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiImplicitParam(name = "ompUserDto", value = "系统用户数据类", required = true)
    public Result login(@RequestBody OmpUserDto ompUserDto) {
        logger.info("用户登录：dto={}", ompUserDto);
        Result result = ompUserService.login(ompUserDto.getLoginName(), ompUserDto.getPassword());
        return result;
    }

    /**
     * 获取用户信息
     *
     * @param token
     * @return
     */
    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    @ApiImplicitParam(name = "token", value = "用户登录令牌", required = true)
    public Result getUserInfo(@RequestParam("token") String token) {
        logger.info("获取用户信息：token={}", token);
        Result result = ompUserService.getUserInfo(token);
        return result;
    }

    /**
     * 获取用户角色路径
     *
     * @param userKey
     * @return
     */
    @GetMapping("/path")
    @ApiOperation("获取用户角色菜单")
    @ApiImplicitParam(name = "userKey", value = "系统用户主键", required = true)
    public Result getUserPath(@RequestParam("userKey") String userKey) {
        logger.info("用户角色路径：userKey={}", userKey);
        Result result = ompUserService.getUserPath(userKey);
        return result;
    }

    /**
     * 更新登录日志
     *
     * @param ompUserDto
     * @return
     */
    @PostMapping("/loginLog")
    @ApiOperation("更新登录日志")
    @ApiImplicitParam(name = "ompUserDto", value = "系统用户数据类", required = true)
    public Result updateLoginInfo(@RequestBody OmpUserDto ompUserDto) {
        logger.info("更新登录日志：ompUserDto={}", ompUserDto);
        Result result = ompUserService.updateLoginInfo(ompUserDto);
        return result;
    }

    /**
     * 登录错误
     *
     * @param ompUserDto
     * @return
     */
    @PostMapping("/loginError")
    @ApiOperation("登录错误")
    @ApiImplicitParam(name = "ompUserDto", value = "系统用户数据类", required = true)
    public Result loginError(@RequestBody OmpUserDto ompUserDto) {
        logger.info("登录错误：ompUserDto={}", ompUserDto);
        Result result = ompUserService.loginError(ompUserDto.getLoginName());
        return result;
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result logout() {
        logger.info("退出登录");
        return Result.ok("退出登录");
    }

    /**
     * 检核密码
     *
     * @param ompUserDto
     * @return
     */
    @PostMapping("/checkPwd")
    @ApiOperation("登录检核密码")
    @ApiImplicitParam(name = "ompUserDto", value = "系统用户数据类", required = true)
    public Result checkPwd(@RequestBody OmpUserDto ompUserDto) {
        logger.info("检核密码：ompUserDto={}", ompUserDto);
        Result result = ompUserService.checkPwd(ompUserDto);
        return result;
    }

    /**
     * 修改密码
     *
     * @param ompUserDto
     * @return
     */
    @PostMapping("/updatePwd")
    @ApiOperation("修改用户密码")
    @ApiImplicitParam(name = "ompUserDto", value = "系统用户数据类", required = true)
    public Result updatePwd(@RequestBody OmpUserDto ompUserDto) {
        logger.info("修改密码：ompUserDto={}", ompUserDto);
        Result result = ompUserService.updatePwd(ompUserDto);
        return result;
    }

}
